package com.zj;

import dev.langchain4j.community.model.dashscope.QwenEmbeddingModel;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentParser;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentByLineSplitter;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.zj.restApi")
public class AIAgentAPP {
    public static void main( String[] args )
    {
        SpringApplication.run(AIAgentAPP.class, args);
    }

    //TODO:以后修改称为定时任务
    @Bean
    public CommandLineRunner initDataToVectorStore(QwenEmbeddingModel qwenEmbeddingModel,
                                                   EmbeddingStore<TextSegment> embeddingStore) throws Exception{
        //以后最好扫描目录
        URL url =getClass().getClassLoader().getResource("rag/a.txt");
        if (url==null) {
            throw new IllegalStateException("资源 rag/a.txt 不存在");
        }
        Path documentPath = Paths.get(url.toURI());

        //以后上线，这个地方，可以是运行时传进来的要读取的目录，文件的地址
        //java -jar xxx.jar  rag/a.txt
        return args -> {
            try {
                //文档加载
                DocumentParser parser=new TextDocumentParser();
                Document doc= FileSystemDocumentLoader.loadDocument(documentPath,parser);
                //文档切分
                //原来RAG3中是按照正则表达式来分割的，这里换成了分行
                List<TextSegment> segments=new DocumentByLineSplitter(100,20).split(doc);//按行分割

                //向量化
                List<Embedding> embeddings=qwenEmbeddingModel.embedAll(segments).content();

                //存储入库
                embeddingStore.addAll(embeddings,segments);
                System.out.printf("已初始化向量：%d 条%n",embeddings.size());
            } catch (Exception e) {
                //打印日志，别让spring启动失败
                e.printStackTrace();
            }
        };
    }
}
